package com.yanqu.road.dao.impl.activity.gourdplant;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.gourdplant.GourdPlantElement;
import com.yanqu.road.entity.activity.gourdplant.GourdPlantUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GourdPlantUserDataDaoImpl extends TempDataObjectDao<GourdPlantUserData> {

    public static String getGourdPlantUserDataSql = "select * from `t_u_activity_gourd_plant_user_data` where `activityId`=? and `userId`=?";
    public GourdPlantUserData getGourdPlantUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getGourdPlantUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertGourdPlantUserDataSql = "insert into `t_u_activity_gourd_plant_user_data`(activityId,userId,score,combId,lianXinNum,combElement,combItems,combTimes) values (?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(GourdPlantUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCombId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLianXinNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCombElement())));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getCombItems(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCombTimes()));
        if (getDb().executeNoneQuery(insertGourdPlantUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateGourdPlantUserDataSql = "update `t_u_activity_gourd_plant_user_data` set score=?,combId=?,lianXinNum=?,combElement=?,combItems=?,combTimes=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(GourdPlantUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCombId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLianXinNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCombElement())));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getCombItems(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCombTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateGourdPlantUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected GourdPlantUserData parseData(ResultSet rs) throws SQLException {
        GourdPlantUserData data = new GourdPlantUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setScore(rs.getLong("score"));
        data.setCombId(rs.getInt("combId"));
        data.setLianXinNum(rs.getInt("lianXinNum"));
        data.setCombElement(JSON.parseObject(rs.getString("combElement"), new TypeReference<List<GourdPlantElement>>() {}));
        data.setCombItems(StringUtils.stringToIntegerList(rs.getString("combItems"), ","));
        data.setCombTimes(rs.getInt("combTimes"));
        return data;
    }
}
